package lab.bmp.tag;

import lab.bmp.genre.*;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import ru.ssau.DatabaseHelper;

/**
 *
 * @author Smike
 */
public class TagBean implements EntityBean {

    EntityContext context;
    DataSource datasource;
    GenrePK genre;

    public void setEntityContext(EntityContext ctx) throws EJBException, RemoteException {
        this.context = ctx;
    }

    public void unsetEntityContext() throws EJBException, RemoteException {
        this.context = null;
    }

    public void ejbRemove() throws RemoveException, EJBException, RemoteException {
        // do nothing, readonly
    }

    public void ejbActivate() throws EJBException, RemoteException {
        // do nothing
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }

    public void ejbLoad() throws EJBException, RemoteException {
        // lazy
    }

    private void initializeIfNeeded() {
        if (genre == null) {
            TagPK key = getCurrentKey();
            Connection connection = getConnection();
            try {
                lab.domain.ID3v1Tag tag = DatabaseHelper.findTag(connection, key.getId());
                genre = new GenrePK(tag.getGenre().getId());
            } catch (SQLException ex) {
                throw new EJBException(ex);
            } finally {
                try {
                    connection.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public void ejbStore() throws EJBException, RemoteException {
        // do nothing, readonly
    }

    TagPK getCurrentKey() {
        return (TagPK) context.getPrimaryKey();
    }

    public Integer getId() {
        return getCurrentKey().getId();
    }

    public GenrePK getGenre() {
        initializeIfNeeded();
        return genre;
    }

    public TagPK ejbCreate(Integer id, Genre genre) throws CreateException {
        throw new UnsupportedOperationException();
    }

    public TagPK ejbFindByPrimaryKey(TagPK key) throws FinderException {
        lab.domain.ID3v1Tag tag;
        Connection connection = getConnection();
        try {
            tag = DatabaseHelper.findTag(connection, key.getId());
            return key;
        } catch (SQLException ex) {
            throw new FinderException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

    public Collection ejbFindAll() throws FinderException {
        Connection connection = getConnection();
        try {
            Collection<lab.domain.ID3v1Tag> tags = DatabaseHelper.findTags(connection);
            Collection<TagPK> keys = new ArrayList<TagPK>();
            for (lab.domain.ID3v1Tag tag : tags) {
                keys.add(new TagPK(tag.getId()));
            }
            return keys;
        } catch (SQLException ex) {
            throw new FinderException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

    Connection getConnection() {
        try {
            return getDataSource().getConnection();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    DataSource getDataSource() {
        if (datasource == null) {
            try {
                datasource = (DataSource) new InitialContext().lookup("oracleDS");
                return datasource;
            } catch (NamingException ex) {
                throw new RuntimeException(ex);
            }
        }
        return datasource;
    }
}
